package com.scrum.managers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scrum.vos.MessageExceptions;
import com.scrum.vos.User;

@Service
@Transactional
public class UserSessionManager implements IUserSessionManager{

	private Map<String, User> userMap = new HashMap<String, User>();
	
	@Override
	public String loginUser(User user) throws Exception{
		
		String sessionToken = user.getId().toString();
		
		if(userMap.get(sessionToken) != null)
		{
			throw new Exception(MessageExceptions.TOKEN_ALREADY_LOGGED);
		}
		
		userMap.put(sessionToken, user);
		return sessionToken;
	}

	@Override
	public void logoutUser(String sessionToken) throws Exception{
			
		if(userMap.get(sessionToken) == null)
		{
			throw new Exception(MessageExceptions.TOKEN_NOT_LOGGED);
		}
		
		userMap.remove(sessionToken);
	}
	
}
